package com.itheima.Service.CheckServicelmpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.SetmealService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDEV;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;



    /**
     * 新增套餐
     *
     * @param setmeal
     */
    @Transactional
    public void list(Setmeal setmeal, List<Integer> checkgroupIds) {
        SetmealDEV setmealDEV = new SetmealDEV();
        BeanUtils.copyProperties(setmeal,setmealDEV);
        setmealMapper.list(setmealDEV);
        Integer setmealId = setmealDEV.getId();
        for (Integer checkGroupId : checkgroupIds) {
            setmealCheckgroupMapper.insert(setmealId,checkGroupId);
        }
        // 重新生成静态页面
        generateSetmealListStaticPage();
        generateSetmealDetailStaticPage(setmealId);
    }

    /**
     * 重新生成查询套餐
     */
    private void generateSetmealListStaticPage() {
        try {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            cfg.setClassForTemplateLoading(this.getClass(), "/templates");
            Template template = cfg.getTemplate("mobile_setmeal.ftl");
            Map<String, Object> dataModel = new HashMap<>();
            List<Setmeal> setmealList = setmealMapper.findAll();
            dataModel.put("setmealList", setmealList); // 确保 setmealList 被正确传递
            File outFile = new File("D:\\code\\bxg-health-parent\\bxg-health-mobile\\src\\main\\resources\\mobile\\pages\\setmeal_list.html");
            Writer out = new FileWriter(outFile);
            template.process(dataModel, out);
            out.close();
        } catch (IOException | TemplateException e) {
            log.error("生成套餐列表静态页面失败", e);
        }
    }

    /**
     * 重新生成查询套餐详情
     * @param setmealId
     */
    private void generateSetmealDetailStaticPage(Integer setmealId) {
        try {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            cfg.setClassForTemplateLoading(this.getClass(), "/templates");
            Template template = cfg.getTemplate("mobile_setmeal_detail.ftl");
            Map<String, Setmeal> dataModel = new HashMap<>();
            dataModel.put("setmeal", findById(setmealId));
            File outFile = new File("D:\\code\\bxg-health-parent\\bxg-health-mobile\\src\\main\\resources\\mobile\\pages\\setmeal_" + setmealId + ".html");
            Writer out = new FileWriter(outFile);
            template.process(dataModel, out);
            out.close();
        } catch (IOException | TemplateException e) {
            log.error("生成套餐详情静态页面失败", e);
        }
    }
    /**
     * 分页查询套餐
     *
     * @param queryPageBean
     * @return
     */
    public PageResult listpage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Setmeal setmeal = new Setmeal();
        setmeal.setName(queryPageBean.getQueryString());
        BeanUtils.copyProperties(queryPageBean, setmeal);
        Page<Setmeal> page = setmealMapper.listpage(setmeal);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐表
     *
     * @param id
     * @return
     */
    public CheckItem readcheck(Integer id) {
        return setmealMapper.readsetmeal(id);
    }

    /**
     * 删除套餐
     *
     * @param id
     */
    public void delectcheck(Integer id) {
        setmealMapper.delect(id);
        setmealCheckgroupMapper.delectsetmeal(id);
    }

    /**
     * 修改套餐
     * @param setmeal
     */
    @Transactional
    public void updatecheck(Setmeal setmeal,List<Integer>checkGroupIds) {
        setmealMapper.update(setmeal);
        Integer setmealId = setmeal.getId();
        setmealCheckgroupMapper.delectsetmeal(setmealId);//删除中间表
        for (Integer checkGroupId : checkGroupIds) {
            setmealCheckgroupMapper.insert(setmealId,checkGroupId);
        }
    }

    /**
     * 生成套餐列表
     * @return
     */
    public List <Setmeal> findAll() {
        return setmealMapper.findAll();
    }

    /**
     * 生成套餐详情
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        //根据套餐id查询关联的检查组
        Setmeal setmeal = setmealMapper.list1(id);
        List<CheckGroup> ARR = setmealMapper.list3(id);
        for (CheckGroup checkGroup : ARR) {
            Integer id1 = checkGroup.getId();
            List<CheckItem> checkItems = setmealMapper.list2(id1);
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(ARR);
        return setmeal;
    }
}
